package com.admin.education.models.Mapper;

import com.admin.education.models.Questions;
import com.admin.education.models.TemplateAnswers;
import com.admin.education.models.TemplateQuestions;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.mapstruct.*;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Mapper
@Slf4j
public abstract class TemplateQuestionMapper {
    @Autowired
    private TemplateAnswerMapper templateAnswerMapper;

    @Mappings({
            @Mapping(target = "id", ignore = true)
    })
    public abstract TemplateQuestions mapFrom(Questions questions);

    @AfterMapping
    public void afterMapping(Questions questions, @MappingTarget TemplateQuestions templateQuestions) {
        if (null != questions.getAnsweres()) {
            List<TemplateAnswers> templateAnswers = templateAnswerMapper.mapFrom(questions.getAnsweres());
            for (TemplateAnswers templateAnswer : templateAnswers) {
                templateAnswer.setTemplateQuestions(templateQuestions);
            }
            templateQuestions.setTemplateAnswers(templateAnswers);
        }
    }
}
